//
//  DSZGZXQTableViewCell.m
//  7.13苏宁框架
//
//  Created by app15 on 15/7/23.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZGZXQTableViewCell.h"
#import "DSZGZXQHeader.h"
#import "DSZTJCommentModel.h"
#import "MJExtension.h"
#import "DSZXQTableViewCell.h"
#import "DSZTJCommentModel.h"
#import "UIImageView+WebCache.h"
@interface  DSZGZXQTableViewCell()<UITableViewDataSource,UITableViewDelegate>

 @property(nonatomic,retain)   NSArray *commentArray;

@end
@implementation DSZGZXQTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

-(NSArray*)commentArray:(NSArray*)array{
    return _commentArray=array;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self=[[[NSBundle mainBundle]loadNibNamed:@"DSZGZXQTableViewCell" owner:nil options:nil]lastObject];
    }
    return self;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *commentArray=[DSZTJCommentModel modelArrayWithDictArray:_commentArray];
    if (commentArray.count<=3) {
        return commentArray.count;
    }else{
        return 3;
    }
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *iden=@"cell";
    tableView.separatorStyle=0;
    DSZXQTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:iden];
    NSArray *array=[DSZTJCommentModel modelArrayWithDictArray: _commentArray];
    DSZTJCommentModel *model=array[indexPath.row];
    if (cell==nil) {
        cell=[[DSZXQTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
    
    if (indexPath.row==0)
    {
        cell.contentNumBtn.hidden=NO;
    }
    if (indexPath.row==(array.count-1)&&_commentArray.count<=3) {
        cell.contentLineLabel.hidden=YES;
    }
    //cell.contentNameLabel.text=model.name;
    NSString *str=model.name;
    CGFloat i=str.length;
    
    CGRect len=cell.contentNameLabel.frame;
    CGRect len1=cell.contentLabel.frame;
    len=CGRectMake(cell.contentNameLabel.frame.origin.x, cell.contentNameLabel.frame.origin.y, 10+i*15, cell.contentNameLabel.frame.size.height);
    len1=CGRectMake(cell.contentNameLabel.frame.origin.x+i*15+10, cell.contentLabel.frame.origin.y,cell.contentLabel.frame.size.width-i*15, cell.contentLabel.frame.size.height);
    cell.contentLabel.frame=len1;
    cell.contentNameLabel.frame=len;
    [cell.contentImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.image]]];
    cell.contentNameLabel.text=[NSString stringWithFormat:@"%@:",model.name];
    [cell.contentNumBtn setTitle:[NSString stringWithFormat:@"%i",array.count] forState:UIControlStateNormal];
    cell.selectionStyle=0;
    cell.contentLabel.text=model.content;
    return cell;

}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    DSZGZXQHeader *view=[[DSZGZXQHeader alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if(_commentArray.count<=3){
        return 0;
    }else{
        UIView *view=[[UIView alloc]init];
        UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(0, 8, 310, 30)];
        lable.textAlignment=NSTextAlignmentRight;
        lable.text=[NSString stringWithFormat:@"查看所有%i条评论",_commentArray.count];
        lable.font=[UIFont systemFontOfSize:15];
        lable.textColor=[UIColor orangeColor];
        [view addSubview:lable];
        //lable.hidden=YES;
        tableView.tableFooterView=view;
        return view;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 44;
}

@end
